package com.accenture.jproxy;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.utils.URIUtils;
import org.mitre.dsmiley.httpproxy.ProxyServlet;

public class ExProxyServlet extends ProxyServlet {

  //UID
  private static final long serialVersionUID = -4638876817239891023L;
	private List<String> blackList;
	private String proxyUri ;
	public ExProxyServlet(List<String> blacklist,String proxyUri) {
		this.blackList = blacklist;
		this.proxyUri = proxyUri;
	}
	
	@Override
	  protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
	      throws ServletException, IOException {
		boolean flag = true;
		String uriStr = servletRequest.getRequestURI();
		for (String blackuri : blackList) {
			if(uriStr.contains(blackuri)) {
				super.targetUri = this.proxyUri;
				URI uri = null;
		        try {
		            uri = new URI(proxyUri);
		        } catch (URISyntaxException e) {
		        }
		        servletRequest.setAttribute(ATTR_TARGET_HOST, null);
		        super.targetHost = URIUtils.extractHost(uri);
		        flag = false;
		        super.service(servletRequest, servletResponse);
		        break;
			}
		}
		if(flag) {
			super.service(servletRequest, servletResponse);
		}
	  }
}

